//
//  DateBaseSingleton.m
//  UI - 豆瓣
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DateBaseSingleton.h"
#import "FMDatabase.h"
#import "userDebark.h"

@interface DateBaseSingleton()

@property(nonatomic, retain)FMDatabase *db;

@end

@implementation DateBaseSingleton
+(instancetype)shareDataBase{
    
    static DateBaseSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DateBaseSingleton alloc] init];
    }
    return dataBase;
    
}



-(void)openDB{

    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName=[doc stringByAppendingPathComponent:@"Douban.sqlite"];
    NSLog(@"fileName = %@",fileName);
    
    //2.获得数据库
    self.db = [FMDatabase databaseWithPath:fileName];
    if ([self.db open]) {
        NSLog(@"====================数据库打开");
    }
}

-(void)createtabe{
    
    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS movie(id INTEGER PRIMARY KEY AUTOINCREMENT, movieName TEXT NOT NULL, pic_url TEXT NOT NULL, movieId TEXT NOT NULL);"];
    
   
    
    
}

-(void)insertMovie: (Movie *)movie{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO movie (movieName, pic_url, movieId) VALUES ('%@', '%@', '%@')",movie.movieName, movie.pic_url,movie.movieId];
    
    [self.db executeUpdate:sql];
    
    
    
}


-(NSMutableArray *)selectMovie{

    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.db executeQuery:@"select * from movie"];
    
    //2.遍历结果集合
    while ([resultSet next]) {
        
        NSInteger idNum = [resultSet intForColumn:@"id"];
        NSString *movieName = [resultSet objectForColumnName:@"movieName"];
        NSString *pic_url = [resultSet objectForColumnName:@"pic_url"];
        NSString *movieId = [resultSet objectForColumnName:@"movieId"];
        NSLog(@"id = %ld ,movieName=%@, pic_url=%@, movieId = %@",idNum,movieName, pic_url, movieId);
        
        Movie *movie = [[Movie alloc] init];
        movie.number = idNum;
        movie.movieName = movieName;
        movie.movieId = movieId;
        movie.pic_url = pic_url;
        
        [array addObject:movie];
    }
    return array;
}

-(void)createRegisterTable{

    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS register(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, passWord TEXT NOT NULL, confilmPassWord TEXT NOT NULL, phoneNum TEXT, e_mail TEXT NOT NULL );"];
    
   
    
}
-(void)insertRegister: (NSString *)name passWord: (NSString *)password confilmPassWord: (NSString *) confilmPassWord phoneNum: (NSString *)phoneNum e_mail: (NSString *)e_mail{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO register(name, passWord, confilmPassWord, phoneNum, e_mail) VALUES('%@', '%@', '%@', '%@', '%@')", name, password, confilmPassWord, phoneNum, e_mail];
    [self.db executeUpdate:sql];
    

    
}
-(NSMutableArray *)selectUser{
    NSMutableArray *array = [NSMutableArray array];
    
    FMResultSet *resultSet = [self.db executeQuery:@"select * from register"];
    
    while ([resultSet next]) {
        
        NSString *nameStr =  [resultSet objectForColumnName:@"name"];;
        
        [array addObject:nameStr];
    }
    return  array;
}

-(NSMutableArray *)selectDebarkUser{
    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.db executeQuery:@"select * from register"];
    
    while ([resultSet next]) {
        
        NSString *nameStr = [resultSet objectForColumnName:@"name"];;
        NSString *passWordStr = [resultSet objectForColumnName:@"passWord"];
        
        userDebark *user = [[userDebark alloc] init];
        user.name = nameStr;
        user.passWord = passWordStr;
        [array addObject:user];
    }
    return  array;
}

-(void)createCollectTable{
    
    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS collect(id INTEGER PRIMARY KEY AUTOINCREMENT, movieName TEXT NOT NULL, userName TEXT NOT NULL, movieId TEXT NOT NULL);"];
    
}

-(void)insertCollectMovieName: (NSString *)movieName userName: (NSString *)userName movieId: (NSString  *)movieId{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO collect(movieName, userName, movieId) VALUES('%@', '%@', '%@')", movieName, userName, movieId];
    [self.db executeUpdate:sql];
   

    
}

-(NSMutableArray *)selectCollect: (NSString *)name{
    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.db executeQuery:@"SELECT  * FROM collect WHERE userName = ?", name];
    
    while ([resultSet next]) {
        
        NSString *nameStr = [resultSet objectForColumnName:@"movieName"];
        [array addObject:nameStr];
    }
    return  array;
}

-(NSMutableArray *)selectMovieCollect{
    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.db executeQuery:@"SELECT  * FROM collect"];
    
    while ([resultSet next]) {
        
        NSString *nameStr = [resultSet objectForColumnName:@"movieId"];
        [array addObject:nameStr];
    }
    
    return  array;
}


-(void)deleteMovie:(NSString *)name
{
    
    NSString *deleteSql = [NSString stringWithFormat:
                           @"DELETE FROM collect WHERE movieName = '%@'", name];
    [self.db executeUpdate:deleteSql];
}


@end
